package com.project.controller.web;


import com.project.common.utils.MyPageHelper;
import com.project.entity.Course;
import com.project.entity.CourseTeach;
import com.project.myBeans.CourseTeacherDetail;
import com.project.myBeans.R;
import com.project.myBeans.TeacherInfoDetail;
import com.project.service.CourseService;
import com.project.service.CourseTeachService;
import com.project.service.TeacherInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/courseteach")
@Api(description = "课程教师关联接口",value = "courseteach")
public class CourseTeachController {

    @Autowired
    CourseTeachService courseTeachService;

    @Autowired
    CourseService courseService;

    @Autowired
    TeacherInfoService teacherInfoService;

    /**
     * wwh 2018/3/21
     * @param courseTeach
     * @return
     */
    @ApiOperation("教师认领课程或者添加教师团队")
    @PostMapping("/add")
    public R add(@RequestBody CourseTeach courseTeach){
        try{
                courseTeachService.add(courseTeach);
                return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wwh 2018/3/22
     * @param courseid
     * @return
     */
    @ApiOperation("根据课程id查找教授这门课的所有教师")
    @GetMapping("/findTeacherListByCourseid/{courseid}")
    public R findTeacherListByCourseid(@PathVariable Integer courseid){
        try{
            Course course = courseService.findById(courseid);
            List<TeacherInfoDetail> teacherInfoDetails = courseTeachService.findTeachersByCourseid(courseid);
            return R.ok().put("course",course).put("teacherList",teacherInfoDetails);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("根据教师id查找此教师所授课程列表")
    @GetMapping("/findCourseListByTeacherid/{teacherid}")
    public R findCourseListByTeacherid(@PathVariable Integer teacherid){
        try{
            List<Course> courseList = courseTeachService.findCourseListByTeacherid(teacherid);
            List<CourseTeacherDetail> courseTeacherDetailList = new ArrayList<CourseTeacherDetail>();
            for(Course course:courseList){
                CourseTeacherDetail courseTeacherDetail = new CourseTeacherDetail();
                courseTeacherDetail.setCourse(course);
                courseTeacherDetail.setTeacherInfoDetail(teacherInfoService.findDetailByTeacherId(course.getCreatorid()));
                courseTeacherDetailList.add(courseTeacherDetail);
            }
            return R.ok().put("data", courseTeacherDetailList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("根据教师id或课程id查找分页")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody CourseTeach courseTeach){
        try{
            List<CourseTeach> courseTeachList = courseTeachService.findByCondition(courseTeach);
            List<CourseTeacherDetail> courseTeacherDetailList = new ArrayList<>();
            for(CourseTeach courseTeach1 : courseTeachList){
                CourseTeacherDetail courseTeacherDetail = new CourseTeacherDetail();
                courseTeacherDetail.setCourseTeach(courseTeach1);
                courseTeacherDetail.setCourse(courseService.findById(courseTeach1.getCourseid()));
                courseTeacherDetail.setTeacherInfoDetail(teacherInfoService.findDetailByTeacherId(courseTeach1.getTeacherid()));
                courseTeacherDetailList.add(courseTeacherDetail);
            }
            return R.ok("查找成功").put("data",courseTeacherDetailList).put("total", MyPageHelper.getTotal(courseTeachList));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    /**
     * @author wwh
     * @date 2018/04/16
     * @param teacherid
     * @return
     */
    @ApiOperation("根据教师id查找不重复的课程")
    @GetMapping("/findCoursesDistinctByTeacherid/{teacherid}")
    public R findCoursesDistinctByTeacherid(@PathVariable Integer teacherid){
        try{
            List<Integer> ids = courseTeachService.findCoursesDistinctByTeacherid(teacherid);
            List<Course> courseList = new ArrayList<>();
            for(Integer id:ids){
                courseList.add(courseService.findDetailById(id));
            }
            return R.ok().put("data",courseList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**

     * @param courseTeach
     * @return
     * @author TateBrown
     * @date 2018/05/25
     */
    @ApiOperation("为授课对象添加别名，年份，学期，和地点")
    @PostMapping("/modifycouseteach")
    public R modifycouseteach(@RequestBody CourseTeach courseTeach) {
        try {
            if (courseTeachService.updateByPrimaryKeySelective(courseTeach) > 0) {
                return R.ok("设置成功");
            } else {
                return R.error("设置失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序错误!");
        }
    }

    /**
     * @param id
     * @return
     * @author TateBrown
     * @date 2018/05/25
     */
    @ApiOperation("根据授课id返回授课详细信息")
    @PostMapping("/Findbycourseteachid/{id}")
    public R Findbycourseteachid(Integer id) {
        try {
            return R.ok().put("data", courseTeachService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }

}
